package net.cyue.web.easyquery.test.spring;

import jakarta.servlet.ServletContext;
import net.cyue.util.ResourceUtil;
import net.cyue.web.easyquery.core.EasyQueryApplication;
import net.cyue.web.easyquery.core.config.ConfigException;
import net.cyue.web.easyquery.core.config.toml.EasyQueryTOMLConfiguration;
import net.cyue.web.easyquery.core.db.data.QueryWrapper;
import net.cyue.web.easyquery.core.http.data.PathInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class EasyQueryApplicationLauncher {

    private final Logger logger = LoggerFactory.getLogger(this.toString());

    @Autowired
    private EasyQueryApplication<ServletContext> app;

    @EventListener(ContextRefreshedEvent.class)
    public void start() throws ConfigException, IOException {
        this.logger.info("运行EasyQueryApplication");
        if (this.app == null) {
            this.logger.warn("未找到EasyQueryApplication实例");
            return;
        }
        // this.app.runByProperties(ResourceUtil.getResourceAsStream("example.properties"));
        // this.app.run(EasyQueryJSONConfiguration.fromStream(ResourceUtil.getResourceAsStream("example.json")));
        this.app.run(EasyQueryTOMLConfiguration.fromStream(ResourceUtil.getResourceAsStream("example.toml")));
        this.app.getContext().registerQuery(
            PathInfo
                .builder()
                .apiPath("/api/v1/userinfo")
                .addQueryParameter("id")
                .addQueryParameter("username")
                .build(),
            QueryWrapper
                .create()
                .table("user")
                .eq("id", "#{id}")
                .eq("username", "#{username}")
        );
        this.app.getContext().registerPageQuery(
            PathInfo
                .builder()
                .apiPath("/api/v1/user/page/{page}")
                .addQueryParameter("pageSize")
                .build(),
            "page",
            "pageSize",
            QueryWrapper
                .builder()
                .table("user")
                .build()
        );
    }
}
